<template>
  <div class="loginHome">
    <div class="Home-bg">
      <img src="../../assets/css/taylor.png" style="width:100%;height:100%">
    </div>
    <div class="logincontainer">
      <h2 style="margin: 20px 100px">航航航后台管理</h2>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="pass">
          <el-input
            v-model="ruleForm.pass"
            type="text"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="checkPass">
          <el-input
            v-model="ruleForm.checkPass"
            type="password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verificationCode">
          <el-input
            v-model="ruleForm.verificationCode"
            type="text"
            autocomplete="off"
             @keydown.native.enter="submitForm('ruleForm')"
          ></el-input>
          <span
            v-html="verificationCodeSvg"
            @click="refreshVerificationCode()"      
          ></span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script >
import { getCaptcha, verifyCaptcha, login } from "../../api/index";

export default {
  data() {
    //用户名验证
    const validateUsername = (rule, value, callback) => {
      //用户名正则，3到16位（字母，数字，下划线，减号）
      let uPattern = /^[a-zA-Z0-9_-]{3,16}$/;
      if (value === "") {
        callback(new Error("请输入用户名!"));
      } else {
        if (uPattern.test(value)) {
          callback();
        } else {
          callback(new Error("用户名请输入3到16位(字母，数字，下划线，减号)!"));
        }
      }
    };
    // 密码验证
    const validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码!"));
      } else {
        callback();
      }
    };
    //验证码验证
    const validateVerificationCode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入验证码!"));
      } else {
        callback();
      }
    };
    return {
      //验证码svg
      verificationCodeSvg: "",
      ruleForm: {
        pass: "",
        checkPass: "",
        verificationCode: "",
      },
      rules: {
        pass: [{ validator: validateUsername, trigger: "blur" }],
        checkPass: [{ validator: validatePassword, trigger: "blur" }],
        verificationCode: [
          { validator: validateVerificationCode, trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    //获取验证码
    this.getVerificationCode();
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //本地校验通过
          //验证码校验
          verifyCaptcha(this.ruleForm.verificationCode).then((res) => {
            if (res.data.state) {
              //验证码通过,校验用户名密码是否存在
              login(this.ruleForm.pass, this.ruleForm.checkPass).then((res) => {
                if (res.data.state) {
                  //loading页面
                  const loading = this.$loading({
                    lock: true,
                    text: "正在加载中!",
                    spinner: "el-icon-loading",
                    background: "rgba(0, 0, 0, 0.7)",
                  });
                  //登陆成功
                  this.$message.success("登陆成功!");
                  //将token保存到本地
                  let { permission, token, userInfo } = res.data;
                  console.log(permission);
                  console.log(userInfo);
                  localStorage.setItem("hanghanghang-token", token);
                  // 调用vuex中定义的方法，获取用户菜单
                  this.$store.dispatch("FETCH_MENULIST").then(() => {});
                  loading.close(); //关闭loading页面
                  //跳转到首页
                  this.$router.push("/");
                } else {
                  //登录失败
                  this.$message.error("用户名或密码错误!");
                }
              });
            } else {
              //再次刷新验证码
              this.refreshVerificationCode();
              this.$message.error("验证码错误，请重新输入验证码!");
            }
          });
        } else {
          //本地校验不通过
          console.log("error submit!!");
          return false;
        }
      });
    },
    //获取验证码
    getVerificationCode() {
      getCaptcha().then((res) => {
        this.verificationCodeSvg = res.data.img;
      });
    },
    //刷新验证码
    refreshVerificationCode() {
      this.getVerificationCode();
    },
  },
};
</script>
<style lang="scss" scoped>
.loginHome {
  height: 100%;
  width: 100%;
  font-size: 14px;
  position: relative;
  /* 设置将超出容器的部分隐藏 */
   overflow-x: hidden;
  .Home-bg {
  width: 100%;
  height: 100%;
  position: relative;
  // background-image: url(../../assets/css/taylor.png) no-repeat;
  // background-size: 100% 100%;
  // margin: auto;
}
.logincontainer {
  position: absolute;
  width: 400px;
  height: 400px;
  background-color:#ffffff;
  top: 170px;
  left: 1000px;
  border-radius: 10px;
  ::v-deep  .el-input__inner {
  width: 200px;
    }
  }
}


</style>